package com.pushbox.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pushbox.common.SessionName;
import com.pushbox.model.GameScore;
import com.pushbox.service.GameScoreService;
import com.pushbox.vo.BaseResult;
import com.pushbox.vo.ListResult;
import com.pushbox.vo.ScoreVo;

@Controller
@RequestMapping("gameScore")
public class GameScoreController {
	@Autowired
	private GameScoreService gameScoreService;

	@RequestMapping("getScores")
	@ResponseBody
	public BaseResult getScores(GameScore gameScore, HttpSession session) {
		ListResult<GameScore> result = new ListResult<GameScore>();
		try {
			gameScore.setUserId((int) session.getAttribute(SessionName.USER_ID));
			result.setDataList(gameScoreService.getScoreListByUserAndGameID(gameScore));
			result.setResultMsg("查询成功！");
		} catch (Exception e) {
			result.setResult(false);
			result.setResultMsg("查询失败！");
		}
		return result;
	}

	/**
	 * 上传分数接口
	 * 
	 * @param userLogin
	 * @return
	 */
	@RequestMapping("setScore")
	@ResponseBody
	public BaseResult setScore(GameScore gameScore, HttpSession session) {
		BaseResult result = new BaseResult();
		try {
			gameScore.setUserId((int) session.getAttribute(SessionName.USER_ID));
			int value = gameScoreService.addGameScore(gameScore);
			if (1 != value) {
				result.setResult(false);
			} else {
				result.setResultMsg("上传分数成功！");
			}
		} catch (Exception e) {
			result.setResult(false);
			result.setResultMsg("服务器异常，请联系昭爷！");
		}
		return result;
	}

	@RequestMapping("getRankScores")
	@ResponseBody
	public BaseResult getRankScores(Integer gameId) {
		ListResult<ScoreVo> result = new ListResult<ScoreVo>();
		try {
			result.setDataList(gameScoreService.selectMaxScoresByGameID(gameId));
			result.setResultMsg("查询成功！");
		} catch (Exception e) {
			result.setResult(false);
			result.setResultMsg("查询失败！");
		}
		return result;
	}
}
